﻿using DevExpress.XtraReports.UI;
using DevTeam;

using System;
using System.Data;

namespace ConfExpert.Forms.ImportWizards.Koliustu
{
    public partial class GP : XImportWizard
    {
        public GP()
        {
            InitializeComponent();
        }

        private void GP_Load(object sender, EventArgs e)
        {
            this.FileDone +=GP_FileDone;
            this.ProcessStart += GP_ProcessStart;
            this.ProcessDone += GP_ProcessDone;

            btnSave.Visibility = DevExpress.XtraBars.BarItemVisibility.Never;
        }

        DataTable dtResult;
        private void GP_ProcessStart()
        {
            dtResult = new DataTable();
            dtResult.TableName = "Koliustu";

            DataSet dsResult = new DataSet();
            dsResult.Tables.Add(dtResult);
        }

        private void GP_ProcessDone()
        {
            string str = dtResult.Serialize();
            ConfExpert.Forms.Rapor.YeniYapi.KoliUstu7C k = new ConfExpert.Forms.Rapor.YeniYapi.KoliUstu7C(ExecuteDataSet("GetKoliUstu7C", str));
            k.ShowPreview();

            //MasterGrid.Init(dtResult);
            //MasterGrid.AddSummary();
            //MasterGrid.BestFitColumns();
        }

        private void GP_FileDone(DataSet ds, string FileName)
        {
            for (int i = 0; i < ds.Tables.Count; i++)
            {
                DataTable dt = ds.Tables[i];
                DataTable dtSheet = ProcessSheet(dt);
                dtResult.Merge(dtSheet);
            }
        }

        private DataTable ProcessSheet(DataTable dt)
        {
            DataTable dtKoliUstu = new DataTable();
            DataRow drCaption = dt.Rows[0];

            for (int i = 0; i < dt.Columns.Count; i++) // Başlıkları set ediyoruz.
            {
                dtKoliUstu.Columns.Add();
                dtKoliUstu.Columns[i].ColumnName = drCaption[i].ToStr();
            }

            for (int j = 1; j < dt.Rows.Count; j++)
                dtKoliUstu.Rows.Add(dt.Rows[j].ItemArray);

            return dtKoliUstu;
        }
    }
}